/**
 * Authors:  Thomas Moriarty    4/7/7
 *           Zeke Kuafman
 *           Cathrine Quintero
 *
 *
 */

import java.util.*;
// glazed lists library
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.matchers.*;

/**
 * This {@link Matcher} only matches users in a predefined set.
 *
 */
public class ViewsToFilesMatcher implements Matcher {

   /** the users to match */
   private Set views = new HashSet();
   private boolean flavorAnswer = true;
   private boolean viewAnswer = true;
   private boolean maskedAnswer = true;

   public ViewsToFilesMatcher(Collection views) {
   // make a defensive copy of the users
       this.views.addAll(views);
   }

   /* This method is utilizes by the matcher.  It sets an answer variable to true if the file type is associated to the selected
   flavor.
   */
   private void checkFlavor(Object o){
     DBFile fi = (DBFile)o;
       String flavor = fi.getFlavor();
       if (FileBrowserGUI.mode.equals("file")){  //default flavor all file types belong to.\
         flavorAnswer = true;
       }

       else if (FileBrowserGUI.mode.equals(flavor)) {  //chosen flavor
         flavorAnswer = true;
       }
       else {
         flavorAnswer = false; //doesn't match
       }

    }

    /* This method is utilizes by the matcher.  It sets an answer variable to true if the file type is associated to the selected
       view/s.
    */
    private void checkView(Object o){
       DBFile fi = (DBFile)o;
       Iterator viewIt = views.iterator();
          if(views.size()==0){
             viewAnswer = true; //no views selected so all files match
          }
             while(viewIt.hasNext()){
                Views v = (Views)viewIt.next();
                   if(v.isFileInView(fi)){
                      viewAnswer = true;  //file matches view/s
                      break;
                      }
                   else{
                      viewAnswer = false;  //deosn't match
                     }
             }
     }

     /* This method is utilizes by the matcher.  It sets an answer variable to true if the file type should be displayed based on it's
     masked attribute.  It is only called when the system is set to hide masked files.
     */
     private void checkMasked(Object o){
        DBFile fi = (DBFile)o;
        if(fi.isMasked()){
           maskedAnswer = false;  //doesn't match
        }
        else{
           maskedAnswer = true;  //matches
        }
     }

   /**
    * Test whether to include or not include the specified file based
    * on whether or not the view/flavor/masked is selected.
    */
   public boolean matches(Object o) {

       boolean returnVal = false;
          if(o == null) return false;
             if(FileBrowserGUI.hideMaskedFiles == true){
                checkMasked(o);
             }
             checkFlavor(o);
             checkView(o);
          if(viewAnswer == true && flavorAnswer == true && maskedAnswer == true){  //if it passes all three checks display it
             return true;
          }
          else{
             return false;
       }
   }
}
